/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.iussoft.portal.tags
 */
package com.apache.portal.tags;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.apache.client.InfoFavoriteCoreClient;
import com.apache.client.InfoMQCoreClient;
import com.apache.client.InfoReleaseCoreCleint;
import com.apache.database.model.Page;
import com.apache.portal.common.enums.InfoMethodEnum;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;

/**
 * description:  收藏与消息标签
 */
@SuppressWarnings("serial")
public class FavoriteOrMqTag extends TagSupport {

	private String doCode;
	private String pageSize;
	private String attrName;

	public int doStartTag() throws JspException {
		try {
			HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
			LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");
			Map<String, Object> params = PortalPubFactory.getInstance().getMethodParams(request);
			if (null != loginUser) {
				params.put("userId", loginUser.getUserId());
				params.put("userEname", loginUser.getUserEname());
			}
			switch (InfoMethodEnum.getName(doCode)) {
			case Flist:
				params.put("sortCode", "keep_info");
				Object favorites = InfoFavoriteCoreClient.getInstance().selectInfo(params);
				request.setAttribute(attrName, favorites);
				break;
			case Fpages:
				params.put("sortCode", "keep_info");
				params.put("pageSize", StrUtil.doNull(pageSize, "10"));
				params.put("pageIndex", StrUtil.doNull(String.valueOf(params.get("pageIndex")), "1"));
				Object favoritePage = InfoFavoriteCoreClient.getInstance().selectInfo(params);
				gotoPageInfo(favoritePage, request);
				break;
			case Fview:
				params.put("sortCode", "keep_info");
				Object favorite = InfoFavoriteCoreClient.getInstance().selectSingle(params);
				if (null != favorite) {
					request.setAttribute(attrName, favorite);
				}
				break;
			case Mlist:
				params.put("sortCode", "notice");
				Object notices = InfoMQCoreClient.getInstance().selectInfo(params);
				request.setAttribute(attrName, notices);
				break;
			case Mpages:
				params.put("sortCode", "notice");
				params.put("toUserEname", loginUser.getUserEname());
				params.put("pageSize", StrUtil.doNull(pageSize, "10"));
				params.put("pageIndex", StrUtil.doNull(String.valueOf(params.get("pageIndex")), "1"));
				Object noticePage = InfoMQCoreClient.getInstance().selectInfo(params);
				gotoPageInfo(noticePage, request);
				break;
			case Mview:
				params.put("sortCode", "notice");
				Object notice = InfoMQCoreClient.getInstance().selectSingle(params);
				if (null != notice) {
					Map<String, Object> info = (Map<String, Object>) notice;
					if ("0".equals(String.valueOf(info.get("noticeStatus")))) {//修改消息为已读
						String sysPass = String.valueOf(params.get("sysPass"));
						params.clear();
						params.put("sysPass", sysPass);
						params.put("sortCode", "notice");
						params.put("noticeId", String.valueOf(info.get("noticeId")));
						params.put("noticeStatus", "1");
						InfoReleaseCoreCleint.getInstance().updateInfo(params);
					}
					request.setAttribute(attrName, notice);
				}
				break;
			case Default:
				break;
			}
		} catch (Exception e) {
			return super.SKIP_BODY;
		}
		return super.EVAL_BODY_INCLUDE;
	}

	private void gotoPageInfo(Object obj, HttpServletRequest request) {
		if (null != obj) {
			Page page = (Page) obj;
			request.setAttribute(attrName, page.getPageObjects());
			request.setAttribute("pageSize", page.getPageSize());
			request.setAttribute("pageIndex", page.getPageNeeded());
			request.setAttribute("total", page.getCount());
		}
	}

	public String getDoCode() {
		return doCode;
	}

	public void setDoCode(String doCode) {
		this.doCode = doCode;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getAttrName() {
		return attrName;
	}

	public void setAttrName(String attrName) {
		this.attrName = attrName;
	}

}
